<template>
  <page-container>
    <search-form
      v-model="searchForm"
      :searchColumns="searchColumns"
      :searchOptions="{ menuSpan: 8 }"
      @on-search="handleSearch">
      <template v-slot:newBtn>
        <el-button
          type="primary"
          @click="addForm('add')"
          >新增分类</el-button
        >
      </template>
    </search-form>

    <avue-table
      :dataList="dataList"
      :loading="loading"
      :page="page"
      :tableColumns="tableColumns"
      @onPageChange="onPageChange">
      <template v-slot:menu="{ row }">
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="addForm('edit', row)"
          circle></el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="delRow(row.id)"
          circle></el-button>
      </template>
    </avue-table>
  </page-container>
</template>

<script>
import tableMixins from '@/mixins/table'
import { deepClone } from '@/utils'
import { mapGetters } from 'vuex'
import {
  getMovieTypeQuery,
  addMovieTypeForm,
  editMovieTypeForm,
  delMovieTypeForm,
} from '@/api/movie'

import { searchColumns, tableColumns } from './config/table-config'
import { allDialogForm } from './config/dialogForm'

export default {
  mixins: [tableMixins],
  data() {
    return {
      dataList: [],
      childList: [],
      searchForm: {},
      dialogForm: {},
    }
  },
  computed: {
    ...mapGetters(['commonData']),
    userList() {
      return this.commonData?.userList ?? []
    },
    searchColumns,
    tableColumns,
    allDialogForm,
  },
  methods: {
    fetchMethod: getMovieTypeQuery,
    delMethod: delMovieTypeForm,
    // 查询参数格式化
    formatSearch() {
      const searchParams = deepClone(this.searchForm)

      return searchParams
    },
    addForm(type, row) {
      let formData = {}
      formData = deepClone(this.dialogForm)
      if (type === 'edit') {
        formData = deepClone(row)
      }
      this.$dialogForm.show({
        title: `${type === 'edit' ? '编辑' : '新增'}分类`,
        width: '50%',
        data: formData,
        option: {
          span: 24,
          labelWidth: 120,
          column: this.allDialogForm,
        },
        callback: async ({ data, close }) => {
          let resData = {}
          if (type === 'add') {
            resData = await addMovieTypeForm(data)
          } else {
            resData = await editMovieTypeForm(data)
          }
          if (resData.code === 0) {
            this.$message.success(`${type === 'add' ? '新增' : '编辑'}成功`)
          }
          close()
          this.getList()
        },
      })
    },
  },
  mounted() {
    this.getList()
  },
}
</script>

<style lang="scss" scoped></style>
